﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.WebControls.UI;
using System.ComponentModel;
using NLBS.Data;
using NLBS.Entity;
using NLBS.Framework;
using System.Web.UI.WebControls;
using System.Web;
namespace NLBS.Web.UI
{

    public class AdminCallBack : CallBack
    {

        protected override void OnLoad(EventArgs e)
        {
            this.ExtendClientID = "admincommon";
            this.Callback += new CallBackEventHanlder(AdminCallBack_Callback);
            base.OnLoad(e);
        }

        void AdminCallBack_Callback(object sender, CallBackEventArgs args)
        {
            AdminCallBackHelp help = new AdminCallBackHelp(this,args.Parameters);
            if (help.ReadyControl != null)
            {
                help.ReadyControl.RenderControl(args.Output);
            }
        }
        
        //private SettingsCallBack setting;
        //private AnnounceCallBack announce;
        //private LinkCallCallBack link;
        //protected override void CreateChildControls()
        //{
        //    this.Controls.Clear();
        //    link = new LinkCallCallBack();
        //    this.Controls.Add(link);
        //    announce = new AnnounceCallBack();
        //    this.Controls.Add(announce);
        //    CategoryCallCack category = new CategoryCallCack();
        //    this.Controls.Add(category);
        //    WordFilterCallBack wordFiler = new WordFilterCallBack();
        //    this.Controls.Add(wordFiler);
        //    setting = new SettingsCallBack();
        //    this.Controls.Add(setting);
        //    AdminCommonCallBack common = new AdminCommonCallBack();
        //    this.Controls.Add(common);
        //    UserGroupCallBack userGroup = new UserGroupCallBack();
        //    this.Controls.Add(userGroup);
        //    base.CreateChildControls();
        //}

    }

    public class AdminCommonCallBack : CallBack
    {
        protected override void OnLoad(EventArgs e)
        {
            this.ExtendClientID = "admincommon";
            this.Callback += new CallBackEventHanlder(AdminCommonCallBack_Callback);
            base.OnLoad(e);
        }

        void AdminCommonCallBack_Callback(object sender, CallBackEventArgs e)
        {
            System.Web.UI.Control settings = this.Page.LoadControl("Control/settings.ascx");
            System.Web.UI.Control wordFilterControl = this.Page.LoadControl("Control/wordFilter.ascx");
            System.Web.UI.Control announceControl = this.Page.LoadControl("Control/announce.ascx");
            System.Web.UI.Control categoryContent = this.Page.LoadControl("Control/categoryContent.ascx");
            System.Web.UI.Control guestbookList = this.Page.LoadControl("control/linkContent.ascx");
            System.Web.UI.Control messageControl = this.Page.LoadControl("Control/message.ascx");
            System.Web.UI.Control linkContent = this.Page.LoadControl("control/linkContent.ascx");
            System.Web.UI.Control misc = this.Page.LoadControl("control/misc.ascx");
            

            switch (e.Parameter)
            {
                case "misc":
                    this.Controls.Add(misc);
                    misc.RenderControl(e.Output);
                    break;

                case "settings":
                    this.Controls.Add(settings);
                    settings.RenderControl(e.Output);
                    break;
                case "updateSettings":
                    string[] setnames = new string[41] { "blogTitle", "blogDescription", "baseURL","logoImage","blogWebMaster",
                              "blogWebMasterEmail","blogLanguage","defaultViewMode","articlePerPageNormal",
                              "articlePerPageList","listEntryPerPage","commentPerPage","commentTimeOrder",
                      "showTrackbackWithComment","showTrackbackPosition","recentArticleList","recentCommentList","enableDynamicCalendar",
                      "enableContentAutoSplit","contentAutoSplitChars","enableRegister","enableSecurityCode","enableTrackbackIn","enableTrackbackOut",
                      "enableComment","maxCommentLength","maxCommentURLCount","enableGuestBook","entryPerPageGuestBook",
                      "styleSheet","imageFolder","smiliesFolder","smiliesPerRow","minPostDuration","enableVisitorRecord",
                      "maxVisitorRecord","enableUpload","uploadSize","uploadPath","uploadTypes","foot" };
                    for (int s = 0; s < setnames.Length; s++)
                    {
                        Setting updateSets = BlogInfo.Instance().GetSetting(setnames[s]);
                        if (updateSets.Value1 == null || updateSets.Value1 == string.Empty)
                        {
                            updateSets.Value0 = Convert.ToInt32(e.Parameters[s + 1]);
                        }
                        else
                        {
                            updateSets.Value1 = e.Parameters[s + 1];
                        }
                        DbProvider.Instance().UpdateSettings(updateSets);
                    }
                    this.Controls.Add(settings);
                    settings.RenderControl(e.Output);
                    break;
                case "message":
                   
                    this.Controls.Add(messageControl);
                    messageControl.RenderControl(e.Output);
                    break;

                case "linkContent":

                    this.Controls.Add(linkContent);
                    linkContent.RenderControl(e.Output);
                    break;
                case "updateLink":

                    Setting updatelink = BlogInfo.Instance().GetSetting("links");
                    updatelink.Value1 = e.Parameters[1];
                    DbProvider.Instance().UpdateSettings(updatelink);
                    this.Controls.Add(linkContent);
                    linkContent.RenderControl(e.Output);
                    break;

                case "announce":
                    this.Controls.Add(announceControl);
                    announceControl.RenderControl(e.Output);
                    break;

                case "updateAnnounce":

                    string[] names = new string[3] { "announce", "announceDate", "announceShow" };
                    string[] values = new string[3] { e.Parameters[2], DateTime.Now.ToString(), e.Parameters[1] };
                    for (int n = 0; n < names.Length; n++)
                    {
                        Setting updateSet = BlogInfo.Instance().GetSetting(names[n]);
                        if (n == 2)
                        {
                            updateSet.Value0 = Convert.ToInt32(values[n]);
                        }
                        else
                        {
                            updateSet.Value1 = values[n];
                        }
                        DbProvider.Instance().UpdateSettings(updateSet);
                    }

                    this.Controls.Add(announceControl);
                    announceControl.RenderControl(e.Output);
                    break;

                case "categoryContent":
                    this.Controls.Add(categoryContent);
                    categoryContent.RenderControl(e.Output);
                    break;
                case "updateCategory":
                    int b = e.Parameters[1].IndexOf(",");
                    if (b > 0)
                    {
                        string[] catList = e.Parameters[1].Split(new char[] { ',' });
                        string[] titleList = e.Parameters[2].Split(new char[] { ',' });
                        string[] hiddenList = e.Parameters[3].Split(new char[] { ',' });
                        string[] lockList = e.Parameters[4].Split(new char[] { ',' });
                        string[] orderList = e.Parameters[5].Split(new char[] { ',' });
                        for (int h = 0; h < catList.Length; h++)
                        {
                            Category updatecat = DbProvider.Instance().GetCategoryByID(Convert.ToInt32(catList[h]));
                            updatecat.CategoryID = Convert.ToInt32(catList[h]);
                            updatecat.Name = titleList[h];
                            updatecat.Hidden = bool.Parse(hiddenList[h]);
                            updatecat.Locked = bool.Parse(lockList[h]);
                            updatecat.Order = int.Parse(orderList[h]);
                            DbProvider.Instance().UpdateCategory(updatecat);

                        }
                        this.Controls.Add(categoryContent);
                        categoryContent.RenderControl(e.Output);
                    }
                    break;
                case "addCategory":
                    Category cat = new Category();
                    cat.Name = e.Parameters[1];
                    cat.Hidden = bool.Parse(e.Parameters[2]);
                    cat.Locked = bool.Parse(e.Parameters[3]);
                    cat.Order = Convert.ToInt32(e.Parameters[4]);
                    DbProvider.Instance().InsertCategory(cat);
                    this.Controls.Add(categoryContent);
                    categoryContent.RenderControl(e.Output);
                    break;

                case "deleteCategory":
                    for (int i = 0; i < e.Parameters.Length; i++)
                    {
                        if (i > 0)
                        {
                            DbProvider.Instance().DeleteCategory(Convert.ToInt32(e.Parameters[i]));
                        }
                    }
                    this.Controls.Add(categoryContent);
                    categoryContent.RenderControl(e.Output);
                    break;

                case "addWordFilter":
                    WordFilter word = new WordFilter();
                    word.Text = e.Parameters[1];
                    word.Replace = e.Parameters[2];
                    word.RegExp = Convert.ToBoolean(e.Parameters[3]);
                    word.Mode = Convert.ToByte(e.Parameters[4]);
                    DbProvider.Instance().InsertWordFilter(word);
                    this.Controls.Add(wordFilterControl);
                    wordFilterControl.RenderControl(e.Output);
                    break;

                case "deleteWordFilter":
                    for (int i = 0; i < e.Parameters.Length; i++)
                    {
                        if (i > 0)
                        {
                            DbProvider.Instance().DeleteWordFilter(Convert.ToInt32(e.Parameters[i]));
                        }
                    }

                    this.Controls.Add(wordFilterControl);
                    wordFilterControl.RenderControl(e.Output);
                    break;

                case "updateWordFilter":
                    int u = e.Parameters[1].IndexOf(",");
                    if (u > 0)
                    {
                        string[] wordList = e.Parameters[1].Split(new char[] { ',' });
                        string[] textList = e.Parameters[2].Split(new char[] { ',' });
                        string[] replaceList = e.Parameters[3].Split(new char[] { ',' });
                        string[] regexpList = e.Parameters[4].Split(new char[] { ',' });
                        string[] modeList = e.Parameters[5].Split(new char[] { ',' });
                        for (int w = 0; w < wordList.Length; w++)
                        {
                            WordFilter updateWordFilter = DbProvider.Instance().GetWordFilterByID(Convert.ToInt32(wordList[w]));
                            updateWordFilter.Text = textList[w];
                            updateWordFilter.Replace = replaceList[w];
                            updateWordFilter.RegExp = bool.Parse(regexpList[w]);
                            updateWordFilter.Mode = Convert.ToByte(modeList[w]);
                            //updateWordFilter.wf_id = Convert.ToInt32(wordList[w]);
                            DbProvider.Instance().UpdateWordFilter(updateWordFilter);

                        }
                        this.Controls.Add(wordFilterControl);
                        wordFilterControl.RenderControl(e.Output);
                    }
                    break;

                case "wordFilter":
                    this.Controls.Add(wordFilterControl);
                    wordFilterControl.RenderControl(e.Output);
                    break;
            }
        }
    }

    public class UserGroupCallBack : CallBack
    {
        protected override void OnLoad(EventArgs e)
        {
            this.ExtendClientID = "adminUserGroup";
            this.Callback += new CallBackEventHanlder(UserGroupCallBack_Callback);
            base.OnLoad(e);
        }

        void UserGroupCallBack_Callback(object sender, CallBackEventArgs e)
        {
            System.Web.UI.Control userGroup = this.Page.LoadControl("control/userGroup.ascx");
            switch (e.Parameter)
            {
                case "userGroupList":
                    this.Controls.Add(userGroup);
                    userGroup.RenderControl(e.Output);
                    break;
                case "addUserGroup":
                    break;
                case "updateUserGroup":
                    string[] selectList = e.Parameters[1].Split(new char[] { ',' });
                    string[] nameList = e.Parameters[2].Split(new char[] { ',' });
                    string[] rightsList = e.Parameters[3].Split(new char[] { ',' });

                    for (int i = 0; i < selectList.Length; i++)
                    {
                        UserGroup updateGroup = DbProvider.Instance().GetUserGroupByID(Convert.ToInt32(selectList[i]));
                        updateGroup.GroupID = Convert.ToInt32(selectList[i]);
                        updateGroup.Name = nameList[i];
                        updateGroup.Rights = rightsList[i];

                        DbProvider.Instance().UpdateUserGroup(updateGroup);
                    }
                    if (e.Parameters.Length > 4)
                    {
                        string[] newGroupList = e.Parameters[4].Split(new char[] { ',' });
                        UserGroup newGroup = new UserGroup();
                        newGroup.Name=newGroupList[0];
                        newGroup.Rights = newGroupList[1];
                        DbProvider.Instance().InsertUserGroup(newGroup);
                    }
                    this.Controls.Add(userGroup);
                    userGroup.RenderControl(e.Output);
                    break;
                case "deleteUserGroup":
                    for (int i = 1; i < e.Parameters.Length; i++)
                    {
                        DbProvider.Instance().DeleteUserGroup(Convert.ToInt32(e.Parameters[i]));
                    }
                   
                    this.Controls.Add(userGroup);
                    userGroup.RenderControl(e.Output);
                    break;
            }
        }
    }

    public class SettingsCallBack : CallBack
    {
        protected override void OnLoad(EventArgs e)
        {
           
            this.Callback += new CallBackEventHanlder(SettingsCallBack_Callback);
            base.OnLoad(e);
        }

       public void SettingsCallBack_Callback(object sender, CallBackEventArgs e)
        {
            
            System.Web.UI.Control settings = this.Page.LoadControl("Control/settings.ascx");
           
            switch (e.Parameter)
            {
                case "settings":
                    this.Controls.Add(settings);
                    settings.RenderControl(e.Output);
                    break;
                case "updateSettings":
                    string[] setnames = new string[41] { "blogTitle", "blogDescription", "baseURL","logoImage","blogWebMaster",
                              "blogWebMasterEmail","blogLanguage","defaultViewMode","articlePerPageNormal",
                              "articlePerPageList","listEntryPerPage","commentPerPage","commentTimeOrder",
                      "showTrackbackWithComment","showTrackbackPosition","recentArticleList","recentCommentList","enableDynamicCalendar",
                      "enableContentAutoSplit","contentAutoSplitChars","enableRegister","enableSecurityCode","enableTrackbackIn","enableTrackbackOut",
                      "enableComment","maxCommentLength","maxCommentURLCount","enableGuestBook","entryPerPageGuestBook",
                      "styleSheet","imageFolder","smiliesFolder","smiliesPerRow","minPostDuration","enableVisitorRecord",
                      "maxVisitorRecord","enableUpload","uploadSize","uploadPath","uploadTypes","foot" };
                    for (int s = 0; s < setnames.Length; s++)
                    {
                        Setting updateSets = BlogInfo.Instance().GetSetting(setnames[s]);
                        if (updateSets.Value1 == null || updateSets.Value1 == string.Empty)
                        {
                            updateSets.Value0 = Convert.ToInt32(e.Parameters[s + 1]);
                        }
                        else
                        {
                            updateSets.Value1 = e.Parameters[s + 1];
                        }
                        DbProvider.Instance().UpdateSettings(updateSets);
                    }
                    this.Controls.Add(settings);
                    settings.RenderControl(e.Output);
                    break;
                case "message":
                    System.Web.UI.Control messageControl = this.Page.LoadControl("Control/message.ascx");
                    this.Controls.Add(messageControl);
                    messageControl.RenderControl(e.Output);
                    break;
            }
        }
    }

    public class LinkCallCallBack : CallBack
    {
       
        protected override void OnLoad(EventArgs e)
        {
            this.ExtendClientID = "linkContent";
            this.Callback += new CallBackEventHanlder(LinkCallCallBack_Callback);
            base.OnLoad(e);
        }

        public void LinkCallCallBack_Callback(object sender, CallBackEventArgs e)
        {
            //string bbbb = HttpContext.Current.Server.MapPath("control/linkContent.ascx");
            System.Web.UI.Control linkContent = this.Page.LoadControl("control/linkContent.ascx");
            switch (e.Parameter)
            {
                case "linkContent":

                    this.Controls.Add(linkContent);
                    linkContent.RenderControl(e.Output);
                    break;
                case "updateLink":
                   
                    Setting updatelink =BlogInfo.Instance().GetSetting("links");
                    updatelink.Value1 = e.Parameters[1];
                    DbProvider.Instance().UpdateSettings(updatelink);
                    this.Controls.Add(linkContent);
                    linkContent.RenderControl(e.Output);
                    break;
            }
        }
    }
    
    public class WordFilterCallBack : CallBack
    {
        protected override void OnLoad(EventArgs e)
        {
            this.ExtendClientID = "textContent";
            this.Callback += new CallBackEventHanlder(WordFilterCallBack_Callback);
            base.OnLoad(e);
        }

        void WordFilterCallBack_Callback(object sender, CallBackEventArgs e)
        {
            System.Web.UI.Control wordFilterControl = this.Page.LoadControl("Control/wordFilter.ascx");
            switch (e.Parameter)
            {
                case "addWordFilter":
                    WordFilter word = new WordFilter();
                    word.Text = e.Parameters[1];
                    word.Replace = e.Parameters[2];
                    word.RegExp = Convert.ToBoolean(e.Parameters[3]);
                    word.Mode = Convert.ToByte(e.Parameters[4]);
                    DbProvider.Instance().InsertWordFilter(word);
                    this.Controls.Add(wordFilterControl);
                    wordFilterControl.RenderControl(e.Output);
                    break;

                case "deleteWordFilter":
                    for (int i = 0; i < e.Parameters.Length; i++)
                    {
                        if (i > 0)
                        {
                            DbProvider.Instance().DeleteWordFilter(Convert.ToInt32(e.Parameters[i]));
                        }
                    }

                    this.Controls.Add(wordFilterControl);
                    wordFilterControl.RenderControl(e.Output);
                    break;

                case "updateWordFilter":
                    int u = e.Parameters[1].IndexOf(",");
                    if (u > 0)
                    {
                        string[] wordList = e.Parameters[1].Split(new char[] { ',' });
                        string[] textList = e.Parameters[2].Split(new char[] { ',' });
                        string[] replaceList = e.Parameters[3].Split(new char[] { ',' });
                        string[] regexpList = e.Parameters[4].Split(new char[] { ',' });
                        string[] modeList = e.Parameters[5].Split(new char[] { ',' });
                        for (int w = 0; w < wordList.Length; w++)
                        {
                            WordFilter updateWordFilter = DbProvider.Instance().GetWordFilterByID(Convert.ToInt32(wordList[w]));
                            updateWordFilter.Text = textList[w];
                            updateWordFilter.Replace = replaceList[w];
                            updateWordFilter.RegExp = bool.Parse(regexpList[w]);
                            updateWordFilter.Mode = Convert.ToByte(modeList[w]);
                            //updateWordFilter.wf_id = Convert.ToInt32(wordList[w]);
                            DbProvider.Instance().UpdateWordFilter(updateWordFilter);

                        }
                        this.Controls.Add(wordFilterControl);
                        wordFilterControl.RenderControl(e.Output);
                    }
                    break;

                case "wordFilter":
                    this.Controls.Add(wordFilterControl);
                    wordFilterControl.RenderControl(e.Output);
                    break;
            }
        }
    }

    public class AnnounceCallBack : CallBack
    {
       
        protected override void OnLoad(EventArgs e)
        {
            this.ExtendClientID = "announceContent";
            this.Callback += new CallBackEventHanlder(AnnounceCallBack_Callback);
            base.OnLoad(e);
        }

        void AnnounceCallBack_Callback(object sender, CallBackEventArgs e)
        {
            System.Web.UI.Control announceControl = this.Page.LoadControl("Control/announce.ascx");
            switch (e.Parameter)
            {
                case "announce":
                    this.Controls.Add(announceControl);
                    announceControl.RenderControl(e.Output);
                    break;

                case "updateAnnounce":

                    string[] names = new string[3] { "announce", "announceDate", "announceShow" };
                    string[] values = new string[3] { e.Parameters[2], DateTime.Now.ToString(), e.Parameters[1] };
                    for (int n = 0; n < names.Length; n++)
                    {
                        Setting updateSet = BlogInfo.Instance().GetSetting(names[n]);
                        if (n == 2)
                        {
                            updateSet.Value0 = Convert.ToInt32(values[n]);
                        }
                        else
                        {
                            updateSet.Value1 = values[n];
                        }
                        DbProvider.Instance().UpdateSettings(updateSet);
                    }

                    this.Controls.Add(announceControl);
                    announceControl.RenderControl(e.Output);
                    break;
            }
        }
    }

    public class CategoryCallCack : CallBack
    {
        protected override void OnLoad(EventArgs e)
        {
           
            this.Callback += new CallBackEventHanlder(CategoryCallCack_Callback);
            base.OnLoad(e);
        }
        
        void CategoryCallCack_Callback(object sender, CallBackEventArgs e)
        {
            
            System.Web.UI.Control categoryContent = this.Page.LoadControl("Control/categoryContent.ascx");
            switch (e.Parameter)
            {
                case "categoryContent":
                    this.Controls.Add(categoryContent);
                    categoryContent.RenderControl(e.Output);
                    break;
                case "updateCategory":
                    int b = e.Parameters[1].IndexOf(",");
                    if (b > 0)
                    {
                        string[] catList = e.Parameters[1].Split(new char[] { ',' });
                        string[] titleList = e.Parameters[2].Split(new char[] { ',' });
                        string[] hiddenList = e.Parameters[3].Split(new char[] { ',' });
                        string[] lockList = e.Parameters[4].Split(new char[] { ',' });
                        string[] orderList = e.Parameters[5].Split(new char[] { ',' });
                        for (int h = 0; h < catList.Length; h++)
                        {
                            Category updatecat = DbProvider.Instance().GetCategoryByID(Convert.ToInt32(catList[h]));
                            updatecat.CategoryID = Convert.ToInt32(catList[h]);
                            updatecat.Name = titleList[h];
                            updatecat.Hidden = bool.Parse(hiddenList[h]);
                            updatecat.Locked = bool.Parse(lockList[h]);
                            updatecat.Order = int.Parse(orderList[h]);
                            DbProvider.Instance().UpdateCategory(updatecat);

                        }
                        this.Controls.Add(categoryContent);
                        categoryContent.RenderControl(e.Output);
                    }
                    break;
                case "addCategory":
                    Category cat = new Category();
                    cat.Name = e.Parameters[1];
                    cat.Hidden = bool.Parse(e.Parameters[2]);
                    cat.Locked = bool.Parse(e.Parameters[3]);
                    cat.Order = Convert.ToInt32(e.Parameters[4]);
                    DbProvider.Instance().InsertCategory(cat);
                    this.Controls.Add(categoryContent);
                    categoryContent.RenderControl(e.Output);
                    break;

                case "deleteCategory":
                    for (int i = 0; i < e.Parameters.Length; i++)
                    {
                        if (i > 0)
                        {
                            DbProvider.Instance().DeleteCategory(Convert.ToInt32(e.Parameters[i]));
                        }
                    }
                    this.Controls.Add(categoryContent);
                    categoryContent.RenderControl(e.Output);
                    break;
            }
        }
    }
}
